﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.WebTest;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Notification.BeginOperationEvent += OnBeginOperation;
            Notification.EndOperationEvent += OnEndOperation;
            Notification.FailOperationEvent += OnFailOperation;
            Notification.DebugMessageEvent += OnDegubMessage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            
            if (fd.ShowDialog() == DialogResult.OK)
                webTestTreeView1.LoadWebTest(fd.FileName);

            if (webTestTreeView1.Nodes.Count > 0)
                webTestTreeView1.Nodes[0].Expand();
        }

        private void webTestTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            propertyGrid1.SelectedObject = ((GenericWebTestNode) e.Node);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.Show();
        }

        private void OnBeginOperation(NodeOperationEventArgs e)
        {
            //Indent
            string message = e.Message;
            message = message.PadLeft(Notification.Depth * 2 + message.Length);

            e.Context.Add("ListBoxItemIndex", listBox1.Items.Add(message));
        }

        private void OnEndOperation(NodeOperationEventArgs e)
        {
            var itemIndex = (int)e.Context["ListBoxItemIndex"];
            var listBoxItem = (string)listBox1.Items[itemIndex];
            
            if (listBoxItem.Length > 182)
                listBoxItem = listBoxItem.Substring(0, 178) + "[..]";

            //Indent
            listBoxItem = listBoxItem.PadLeft(Notification.Depth * 2 + listBoxItem.Length);

            //Dots between message and word Done
            listBoxItem = listBoxItem.PadRight(185, '.') + "Done";

            listBox1.Items[itemIndex] = listBoxItem;
        }

        private void OnFailOperation(NodeOperationEventArgs e)
        {
            int itemIndex = listBox1.Items.Add(e.Message);
            e.Context.Add("ListBoxItemIndex", itemIndex);

            MessageBox.Show(e.OperationException.ToString());
        }

        private void OnDegubMessage(NodeOperationEventArgs e)
        {
            string message = e.Message;
            message = message.PadLeft(Notification.Depth * 2 + message.Length);
         
            int itemIndex = listBox1.Items.Add(message);
            e.Context.Add("ListBoxItemIndex", itemIndex);
        }

        private void webTestTreeView1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {

        }

        private void webTestTreeView1_DragLeave(object sender, EventArgs e)
        {

        }

        private void webTestTreeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {

        }

        private void webTestTreeView1_DragDrop(object sender, DragEventArgs e)
        {

        }

        private void webTestTreeView1_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void webTestTreeView1_DragEnter(object sender, DragEventArgs e)
        {

        }

        private void webTestTreeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {

        }




        
    }
}
